<script setup lang="ts">
import { computed, reactive, watch } from 'vue';
import { $t } from '@/locales';
import type { LogOperate } from '@/service/api';
import { DICT_TYPE, getDictObj } from '@/utils/dict';

defineOptions({
  name: 'LogLoginDetailModel'
});

interface Props {
  /** the edit row data */
  rowData?: LogOperate | null;
}

const props = defineProps<Props>();

const visible = defineModel<boolean>('visible', {
  default: false
});

const title = computed(() => {
  return $t('common.detail');
});

type Model = Pick<
  LogOperate,
  | 'id'
  | 'traceId'
  | 'userId'
  | 'userType'
  | 'nickname'
  | 'applicationName'
  | 'javaMethod'
  | 'requestMethod'
  | 'requestParams'
  | 'requestBody'
  | 'responseBody'
  | 'requestUrl'
  | 'userIp'
  | 'userAgent'
  | 'operateModule'
  | 'operateName'
  | 'operateType'
  | 'beginTime'
  | 'endTime'
  | 'duration'
  | 'resultCode'
  | 'resultMsg'
>;

const model: Model = reactive(createDefaultModel());

function createDefaultModel(): Model {
  return {
    id: 0,
    traceId: '',
    userId: '',
    userType: 0,
    nickname: '',
    applicationName: '',
    javaMethod: '',
    requestMethod: '',
    requestParams: '',
    requestBody: '',
    responseBody: '',
    requestUrl: '',
    userIp: '',
    userAgent: '',
    operateModule: '',
    operateName: '',
    operateType: null,
    beginTime: null,
    endTime: null,
    duration: null,
    resultCode: '',
    resultMsg: ''
  };
}

function handleInitModel() {
  Object.assign(model, createDefaultModel());

  if (props.rowData) {
    Object.assign(model, props.rowData);
  }
}

function closeDrawer() {
  visible.value = false;
}

watch(visible, () => {
  if (visible.value) {
    handleInitModel();
  }
});
</script>

<template>
  <NModal v-model:show="visible" :title="title" preset="card" class="w-320">
    <NScrollbar class="max-h-200 pr-20px">
      <NDescriptions :column="2" label-placement="left" bordered label-style="width: 120px">
        <NDescriptionsItem :label="$t('page.manage.log.operate.id')" content-style="width: 30%">
          {{ model.id }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.manage.log.operate.traceId')" content-style="width: 40%">
          {{ model.traceId }}
        </NDescriptionsItem>
      </NDescriptions>
      <NDescriptions :column="2" label-placement="left" bordered label-style="width: 120px">
        <NDescriptionsItem :label="$t('page.manage.log.operate.operateModule')" content-style="width: 30%">
          {{ model.applicationName }} / {{ model.operateModule }} / {{ model.operateName }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.manage.log.operate.requestUrl')" content-style="width: 40%">
          {{ model.requestUrl }}
        </NDescriptionsItem>
      </NDescriptions>
      <NDescriptions :column="2" label-placement="left" bordered label-style="width: 120px">
        <NDescriptionsItem :label="$t('page.manage.log.operate.userInfo')" content-style="width: 30%">
          {{ model.nickname }}
          ( {{ getDictObj(DICT_TYPE.USER_TYPE, model.userType)?.label }} ) / {{ model.userIp }} / {{ model.userAgent }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.manage.log.operate.requestMethod')" content-style="width: 40%">
          {{ model.requestMethod }}
        </NDescriptionsItem>
      </NDescriptions>

      <NDescriptions :column="1" label-placement="left" bordered label-style="width: 120px">
        <NDescriptionsItem :label="$t('page.manage.log.operate.javaMethod')">
          {{ model.javaMethod }}
        </NDescriptionsItem>
      </NDescriptions>
      <NDescriptions :column="1" label-placement="left" bordered label-style="width: 120px">
        <NDescriptionsItem :label="$t('page.manage.log.operate.requestParams')">
          {{ model.requestParams == null ? model.requestBody : model.requestParams }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.manage.log.operate.responseBody')">
          {{ model.responseBody }}
        </NDescriptionsItem>
      </NDescriptions>
      <NDescriptions :column="3" label-placement="left" bordered label-style="width: 120px" content-style="width: 20%">
        <NDescriptionsItem :label="$t('page.manage.log.operate.resultMsg')">
          {{ model.resultMsg }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.manage.log.operate.duration')">{{ model.duration }} ms</NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.manage.log.operate.beginTime')">
          {{ model.beginTime }}
        </NDescriptionsItem>
      </NDescriptions>
    </NScrollbar>
    <template #footer>
      <NSpace justify="end" :size="16">
        <NButton @click="closeDrawer">{{ $t('common.close') }}</NButton>
      </NSpace>
    </template>
  </NModal>
</template>

<style scoped></style>
